﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfBeyond.pages
{
    /// <summary>
    /// PageHistory.xaml 的交互逻辑
    /// </summary>
    public partial class PageHistory : Page
    {
        List<TaskInfo> list = new List<TaskInfo>();
        string path = "";

        public PageHistory()
        {
            InitializeComponent();

        }

        private void BtnTaskDetail_Click(object sender, RoutedEventArgs e)
        {
            int Index = 0;
            TaskInfo t = null;
            Button btn = sender as Button;

            string ebmid = btn.Tag.ToString();

            for (Index = list.Count - 1; Index >= 0; Index--)
            {
                if (list[Index] == null) break;

                if (list[Index].Ebmid_str == ebmid)
                {
                    t = list[Index];
                }
            }

            if (t == null)
            {
                Common.ShowMessage("提示", "任务信息错误！");
                return;
            }
            WindowTaskDetail w = new WindowTaskDetail(t);
            w.Show();
        }

        private void load_histroy()
        {
            DateTime dt = DateTime.MinValue;
            if (sel_dates.SelectedDate.HasValue)
            {
                dt = sel_dates.SelectedDate.Value;
            }
            else
                return;

            path = @"history/" + dt.ToShortDateString() + ".json";

            try
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    string output = sr.ReadToEnd();
                    list = JsonConvert.DeserializeObject<List<TaskInfo>>(output);
                    if (list != null)
                    {
                        grid_history.ItemsSource = null;
                        grid_history.ItemsSource = list;
                    }
                }
            }
            catch (Exception ex)
            {
                grid_history.ItemsSource = null;
                LogHelp.Error(ex.ToString());
            }
        }

        private void search_task_Click(object sender, RoutedEventArgs e)
        {
            load_histroy();
        }

        private void sel_dates_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (Mouse.Captured is System.Windows.Controls.Primitives.CalendarItem)
            {
                Mouse.Capture(null);
            }
        }
    }



}
